package zhuyun.code.launcheractivity.view

import android.content.Intent
import android.graphics.Bitmap
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_sign_in_first.*
import kotlinx.android.synthetic.main.big_code.*
import kotlinx.android.synthetic.main.et_img_hint.view.*
import kotlinx.android.synthetic.main.title_bar_base.*
import zhuyun.code.launcheractivity.R
import zhuyun.code.launcheractivity.model.Constants
import zhuyun.code.launcheractivity.model.NetworkRequest
import zhuyun.code.launcheractivity.model.ResponseDataHandler
import zhuyun.code.launcheractivity.model.data.BaseResponse
import zhuyun.code.launcheractivity.utils.CodeUtils
import kotlin.properties.Delegates

class SignInFirstActivity : BaseActivity(),View.OnClickListener {
    private var phoneNumberNotEmpty:Boolean = false
    private var verifyCodeNotEmpty:Boolean = false
    private var passwordNotEmpty:Boolean = false
    private var comfirmNotEmpty:Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sign_in_first)
        tvRight.text = "切换语言"
        iv_back.setOnClickListener(this)
        btContinue.setOnClickListener(this)
        ivCode.setOnClickListener(this)
        tv_login.setOnClickListener(this)
        tv_agreement.setOnClickListener(this)
        etHint_phone.et.addTextChangedListener(object :TextWatcher{
            override fun afterTextChanged(p0: Editable?) {

            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int,before: Int, count: Int) {
                phoneNumberNotEmpty = s!!.isNotEmpty()
                btContinue.isEnabled = phoneNumberNotEmpty&&verifyCodeNotEmpty&&passwordNotEmpty&&comfirmNotEmpty
            }

        })
        etHint_password.et.addTextChangedListener(object :TextWatcher{
            override fun afterTextChanged(p0: Editable?) {

            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                passwordNotEmpty = s!!.isNotEmpty()
                btContinue.isEnabled = phoneNumberNotEmpty&&verifyCodeNotEmpty&&passwordNotEmpty&&comfirmNotEmpty
            }

        })
        etHint_password2.et.addTextChangedListener(object :TextWatcher{
            override fun afterTextChanged(p0: Editable?) {

            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                comfirmNotEmpty = s!!.isNotEmpty()
                btContinue.isEnabled = phoneNumberNotEmpty&&verifyCodeNotEmpty&&passwordNotEmpty&&comfirmNotEmpty
            }


        })
        etHint_code.et.addTextChangedListener(object :TextWatcher{
            override fun afterTextChanged(p0: Editable?) {

            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                verifyCodeNotEmpty = s!!.isNotEmpty()
                btContinue.isEnabled = phoneNumberNotEmpty&&verifyCodeNotEmpty&&passwordNotEmpty&&comfirmNotEmpty
            }

        })
        etHint_password.tv.textSize = 11f
        etHint_password2.tv.textSize = 11f
        getCode()


    }
    private lateinit var bitmap:Bitmap

    fun getCode(){
        bitmap = CodeUtils.createBitmap()
        ivCode.setImageBitmap(bitmap)
    }

    override fun onClick(v: View?) {
        when(v!!.id){
            R.id.iv_back->{finish()}
            R.id.btContinue->{
                if(etHint_password.et.text.toString().trim() != etHint_password2.et.text.toString().trim()){
                    Toast.makeText(this,"两次输入的密码不一样",Toast.LENGTH_SHORT).show()
                    return
                }
                if(CodeUtils.code != etHint_code.et.text.toString().trim()){
                    Toast.makeText(this,"验证码错误",Toast.LENGTH_SHORT).show()
                    return
                }
                NetworkRequest.run {
                    verificationCode(etHint_phone.et.text.toString().trim(),1,1,etHint_code.et.text.toString().trim()
                                ,object :ResponseDataHandler<String>(){
                            override fun onSuccess(
                                resultData: String?,
                                baseResponse: BaseResponse<String>
                            ) {
                                register(etHint_phone.et.text.toString().trim(),etHint_password.et.text.toString().trim())
                            }

                            override fun onFailure(errMsg: String?) {
                                Toast.makeText(this@SignInFirstActivity,errMsg,Toast.LENGTH_SHORT).show()
                            }
                        })
                }


            }
            R.id.ivCode->{
                val dialog = WLCommonDialog(this)
                dialog.iv.setImageBitmap(bitmap)
                dialog.ivFresh.setOnClickListener {
                    getCode()
                    dialog.iv.setImageBitmap(bitmap)
                }
                dialog.setCanceledOnTouchOutside(true)
                dialog.show()

            }
            R.id.tv_login->{
                startActivity(Intent(this,LoginActivity::class.java))
            }

        }

    }



    fun register(userName:String, password:String){
        NetworkRequest.phoneRegister(userName,password,etHint_code.et.text.toString().trim(),Constants.country_id,0,
            "","","",etHint_extend_code.et.text.toString().trim(),
            object :ResponseDataHandler<String>(){
                override fun onSuccess(resultData: String?, baseResponse: BaseResponse<String>) {

                }

                override fun onFailure(errMsg: String?) {
                    Toast.makeText(this@SignInFirstActivity,errMsg,Toast.LENGTH_SHORT).show()
                }

            })
    }
}
